package org.neuedu.vblogserver.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.neuedu.vblogserver.bean.RespBean;
import org.neuedu.vblogserver.bean.Role;
import org.neuedu.vblogserver.bean.User;
import org.neuedu.vblogserver.bean.UserBean;
import org.neuedu.vblogserver.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService implements UserDetailsService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userMapper.loadUserByUsername(s);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        List<Role> roles = userMapper.getRolesById(user.getId());
        user.setRoles(roles);
        return user;
    }

    public PageInfo getUsers(String serchKeyWord, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserBean> users = userMapper.getUsers(serchKeyWord);
        for (UserBean user : users) {
            List<Role> roles = userMapper.getRolesById(user.getId());
            user.setRoles(roles);
            List<Long> rid = new ArrayList<>();
            for (Role role : roles) {
                rid.add(role.getId());
            }
            user.setRid(rid);
        }
        PageInfo pageInfo = new PageInfo(users);
        return pageInfo;
    }

    public RespBean isEnabled(UserBean userBean) {
        User loginer = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (loginer.getId().toString().equals(userBean.getId().toString())) {
            return RespBean.error(500, "你处于登录状态，不能修改自己");
        } else {
            int i = userMapper.isEnabled(userBean);
            if (i == 0) {
                return RespBean.error(500, "操作失败");
            } else {
                return RespBean.success(200, "操作成功");
            }
        }
    }

    public RespBean isLocked(UserBean userBean) {
        User loginer = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (loginer.getId().toString().equals(userBean.getId().toString())) {
            return RespBean.error(500, "你处于登录状态，不能修改自己");
        } else {
            int i = userMapper.isLocked(userBean);
            if (i == 0) {
                return RespBean.error(500, "操作失败");
            } else {
                return RespBean.success(200, "操作成功");
            }
        }
    }

    public RespBean delUserRole(String uid, String rid) {
        User loginer = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (loginer.getId().toString().equals(uid)) {
            return RespBean.error(500, "你处于登录状态，不能修改自己");
        } else {
            int counts = userMapper.selectRoleCount(Integer.valueOf(uid));
            if (counts > 1) {
                int i = userMapper.delUserRole(Integer.valueOf(uid), Integer.valueOf(rid));
                if (i == 0) {
                    return RespBean.error(500, "修改个人权限失败");
                } else {
                    return RespBean.success(200, "修改个人权限成功");
                }
            } else {
                return RespBean.error(500, "用户只有一个权限，不能在删除了");
            }
        }
    }
}